package com.tianhui.consignortwofei.mvp.ui.fragment;

import android.os.Bundle;

import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseDataListFragment;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.mvp.model.OrderModel;
import com.tianhui.consignortwofei.mvp.model.enty.DispatchDetailInfo;
import com.tianhui.consignortwofei.mvp.model.enty.OrderInfo;
import com.tianhui.consignortwofei.mvp.ui.adapter.DispatchDetailListAdapter;

import org.yczbj.ycrefreshviewlib.adapter.RecyclerArrayAdapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fgs
 * @date 2020/9/7
 * @description
 **/
public class DispatchDetailListFragment extends BaseDataListFragment {

    private OrderInfo mOrderInfo;
    private OrderModel mOrderModel;

    public DispatchDetailListFragment() {
        // Required empty public constructor
    }

    @Override
    protected boolean isNeedLazyLoad() {
        return false;
    }

    public static DispatchDetailListFragment newInstance() {
        DispatchDetailListFragment fragment = new DispatchDetailListFragment();
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_dispatch_detail_list;
    }

    @Override
    protected CommonView createView() {
        return null;
    }

    @Override
    protected CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public RecyclerArrayAdapter createAdapter() {
        return new DispatchDetailListAdapter(getContext());
    }

    @Override
    protected void initListener() {

    }

    @Override
    public void requestDataList(int page, boolean isDialog) {
        if (mOrderInfo != null) {

            Map<String, String> map = new HashMap<>();
            map.put("dispatchno", mOrderInfo.dispatchno);
            mOrderModel.dispatchDetailListModel(getContext(), map, isDialog, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<List<DispatchDetailInfo>>>() {
                @Override
                public void onNext(CommonResponse<List<DispatchDetailInfo>> commonResponse) {
                    if (commonResponse.getCode() == 20000) {
                        List<DispatchDetailInfo> list = commonResponse.getData();
                        if (list != null && list.size() > 0) {
                            mAdapter.clear();
                            mAdapter.addAll(list);
                        } else {
                            showEmptyView();
                        }
                    } else {
                        showToast(commonResponse.getMsg());
                        showEmptyView();
                    }
                }

                @Override
                public void onError(ExceptionHandle.ResponseThrowable e) {
                    showToast(e.message);
                    showEmptyView();
                }
            });
        }
    }

    @Override
    public void initData() {
        Bundle bundle = getArguments();
        if (bundle != null && !bundle.isEmpty()) {
            mOrderInfo = bundle.getParcelable(KeyValue.KEY_ORDER_INFO);
        }
        mOrderModel = new OrderModel();

    }
}
